import '../../assets/css/reset.css';
import '../../assets/font/iconfont.css';
import './index.css';

var BASE_URL = 'http://120.76.136.131:3554/madp-html-cache';

$(function () {
  try{
    loadNativeObject('NativeNetwork');
  }catch (e) {
    console.error(e);
  }
  if(typeof(NativeNetwork) !== 'undefined'){
    alert('原生请求网络');
    NativeNetwork.sendRequest({
      'url': 'T_SJCZ_1.hsml?flowsn=5000',
      'params':{
        'startDate':'2018-06-01',
        'endDate': '2018-09-30'
      },
      'handler':function(response, error){
        if(error){
          $('.icon-loadding').css({'opacity': 0, 'display': 'none'});
          alert(error);
        }else{
          alert(response);
          response = JSON.parse(response);
          load(response);
        }
      }
    },function(response, error){
      if(error){
        $('.icon-loadding').css({'opacity': 0, 'display': 'none'});
        alert(error);
      }else{
        alert(response);
        response = JSON.parse(response);
        load(response);
      }
    });

  }else{
    alert('AJAX请求网络');
    $.ajax({
      type: 'POST',
      url: BASE_URL + '/api/home.do'
    }).then(function (response) {
      load(response);
    })
  }


  /**
   * 获取到数据页面渲染
   */
  function load(response){
    // loadding隐藏
    $('.icon-loadding').css({'opacity': 0, 'display': 'none'});
    // 获取轮播图数据
    var banners = response.data.banners,bannersTags = '';
    for(var i = 0; i < banners.length; i ++){
      bannersTags += '<div class="swiper-slide"><img src="' + BASE_URL + '/h5' +  banners[i].image + '" /><div>' + banners[i].title + '</div></div>';
    }
    $('.swiper-wrapper').html(bannersTags);
    new Swiper('.swiper-container', {
      pagination: '.swiper-pagination',
      autoplay: 5000,//可选选项，自动滑动
    });

    // 获取功能容器节点
    var funs = response.data.funs, funsTags = '';
    for(var i = 0; i < funs.length; i ++){
      if((i % 4) == 0){
        funsTags += '<div class="fun-icon-row">';
      }
      funsTags += '<div class="fun-icon-col">';
      funsTags += '<i class="icon iconfont ' + funs[i].icon + '"></i>';
      funsTags += '<div>' + funs[i].title + '</div>';
      funsTags += '</div>';
      if((i % 4) == 3){
        funsTags += '</div>';
      }
    }
    $('.fun-icon-container').html(funsTags);

    // 获取理财产品数据
    var prods = response.data.prods, prodsTags = '';
    for(var i = 0; i < prods.length; i ++){
      prodsTags += '<div class="prod-list-row">';
      prodsTags += '<div class="prod-list-left">';
      prodsTags += '<div>' + prods[i].prodName + '</div>';
      prodsTags += '<div>' + prods[i].prodDesc + '</div>';
      prodsTags += '</div>';
      prodsTags += '<div class="prod-list-right">';
      prodsTags += '<div>' + prods[i].income + '</div>';
      prodsTags += '<div>七年年化收益率</div>';
      prodsTags += '</div>';
      prodsTags += '</div>';
    }
    $('.prod-list-container').html(prodsTags);
  };
});
